package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.List;

import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.adapter.CommonAdapter;
import liuliu.throughwaring.adapter.ViewHolder;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.model.PersonModel;
import liuliu.throughwaring.model.PoliceModel;

/**
 * 民警信息管理页面
 * 作者：柳伟杰 on 2016/3/23 16:15
 * 邮箱：1031066280@qq.com
 */
public class PoliceAndLiuDongActivity extends BaseActivity {
    public static PoliceAndLiuDongActivity mInstance;
    @CodeNote(id = R.id.police_toolbar)
    TToolbar police_toolbar;
    @CodeNote(id = R.id.entry_police_name_et)
    EditText entry_police_name_et;
    @CodeNote(id = R.id.add_police_btn, click = "onClick")
    Button add_police_btn;
    @CodeNote(id = R.id.no_data_ll)
    LinearLayout no_data_ll;
    @CodeNote(id = R.id.police_lv)
    ListView police_lv;
    CommonAdapter<PoliceModel> police_mAdapter;
    CommonAdapter<PersonModel> person_mAdapter;
    String style = "";

    @Override
    public void initViews() {
        setContentView(R.layout.activity_police);
        mInstance = this;
        style = getIntent().getStringExtra("style");
        police_toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                mInstance.finish();
            }
        });
        loadPolice();
    }

    @Override
    public void initEvents() {
        switch (style) {
            case "liudong":
                police_toolbar.setLeftTitle("流动人员");
                entry_police_name_et.setHint("请输入人员信息");
                break;
            case "police":
                police_toolbar.setLeftTitle("民警管理");
                entry_police_name_et.setHint("请输入民警信息");
                break;
        }
        entry_police_name_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {//文本数据框文字改变
                loadPolice();
            }
        });
    }

    boolean nodata = false;

    /**
     * 根据条件查询警员或流动人员信息
     */
    private void loadPolice() {
        switch (style) {
            case "liudong":
                final List<PersonModel> liudong = finalDb.findAllByWhere(PersonModel.class, "PersonName like '%" + entry_police_name_et.getText().toString()
                        + "%' and PersonComapnyType='Free'");
                if (liudong != null) {
                    if (liudong.size() > 0) {
                        nodata = true;
                        person_mAdapter = new CommonAdapter<PersonModel>(mInstance, liudong, R.layout.item_liudong) {
                            @Override
                            public void convert(ViewHolder holder, final PersonModel person, int position) {
                                holder.setText(R.id.user_name_tv_liudong, person.getPersonName());
                                holder.setText(R.id.create_time_tv_liudong, person.getPersonCreateTime());
                                holder.setText(R.id.user_unit_tv_liudong, person.getPersonAddress());
                                holder.setOnClickListener(R.id.edit_btn_liudong, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        //跳转到详细页面
                                        mUtils.IntentPost(PersonDetailActivity.class, new Utils.putListener() {
                                            @Override
                                            public void put(Intent intent) {
                                                intent.putExtra("personid", person.getPersonId());
                                            }
                                        });
                                    }
                                });

                            }
                        };
                        police_lv.setAdapter(person_mAdapter);
                        police_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                                //跳转到详细页面
                                mUtils.IntentPost(PersonDetailActivity.class, new Utils.putListener() {
                                    @Override
                                    public void put(Intent intent) {
                                        intent.putExtra("personid", liudong.get(position).getPersonId());
                                    }
                                });
                            }
                        });
                        police_lv.setVisibility(View.VISIBLE);
                        no_data_ll.setVisibility(View.GONE);
                    } else {
                        police_lv.setVisibility(View.GONE);
                        no_data_ll.setVisibility(View.VISIBLE);
                    }
                }
                break;
            case "police":
                final List<PoliceModel> polices = finalDb.findAllByWhere(PoliceModel.class, "PoliceName like '%" + entry_police_name_et.getText().toString() + "%'");
                if (polices != null) {
                    if (polices.size() > 0) {
                        nodata = true;
                        police_mAdapter = new CommonAdapter<PoliceModel>(mInstance, polices, R.layout.item_police) {
                            @Override
                            public void convert(ViewHolder holder, final PoliceModel policeModel, int position) {
                                holder.setText(R.id.police_name_tv, policeModel.getPoliceName());
                                holder.setText(R.id.police_tel_tv, policeModel.getPolicePhoneNumber());
                                holder.setOnClickListener(R.id.call_btn, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + policeModel.getPolicePhoneNumber()));
                                        startActivity(intent);
                                    }
                                });
                            }
                        };
                        police_lv.setAdapter(police_mAdapter);
                        police_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                                mUtils.IntentPost(PoliceDetailActivity.class, new Utils.putListener() {
                                    @Override
                                    public void put(Intent intent) {
                                        intent.putExtra("policeId", polices.get(position).getPoliceId());
                                    }
                                });
                            }
                        });
                        police_lv.setVisibility(View.VISIBLE);
                        no_data_ll.setVisibility(View.GONE);
                    } else {
                        police_lv.setVisibility(View.GONE);
                        no_data_ll.setVisibility(View.VISIBLE);
                    }
                }
                break;
        }
    }


    public void onClick(View view) {
        Intent intent;
        Bundle bundle;
        switch (view.getId()) {
            case R.id.add_police_btn://添加民警按钮
                switch (style) {
                    case "liudong":
                        intent = new Intent();
                        bundle = new Bundle();
                        intent.setClass(PoliceAndLiuDongActivity.this, RegPersonActivity.class);
                        bundle.putString("companyName", style);
                        intent.putExtras(bundle);
                        startActivityForResult(intent, 0);
                        break;
                    case "police":
                        intent = new Intent();
                        intent.setClass(PoliceAndLiuDongActivity.this, RegPoliceActivity.class);
                        startActivityForResult(intent, 0);
                        break;
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (resultCode == 133 || resultCode == 131) {
                entry_police_name_et.setText("");
                loadPolice();
            }
        }
    }
}
